# Social Learning Game
# Correlates and Validity
# Prepared by Tim Waring
# for publication
# 2020.08

library(tidyverse)
library(gridExtra)
library(ggridges)
library(broom)


## read in merged survey data (surveys 1 & 2, N = 37)
data <- read_csv("//Users/twaring/Documents/Research/CSP/data/surveys 1 and 2 - overlap only.csv") ; head(data)

# clean the data
data$Survey_1_num <- as.numeric(data$Survey_1_num)
data$Synd_name <- as.factor(data$Synd_name)

# collect correlations using cor.test()
cor.test(data$Game_buycount, data$migrate, method = 'pearson')
tidy(cor.test(data$Game_buycount, data$migrate, method = 'spearman'))

cortest <- function (x,y,method, alternative,...) {
		return(tidy(cor.test(x=x,y=y,method=method, alternative=alternative)))
}

cortest(x=data$Game_buycount, y=data$migrate, method = 'pearson', alternative ='greater')


length(names(data))

multi <- function (x,df, mthd, alt) {
	tb <- tibble()
	varlist <- names(df)
	for (i in c(1:length(names(df))) ) {
		pred <- df[[i]]
		print(names(df[i]))
		newrow <- cortest(x=x, y=pred, method = mthd, alternative = alt)
		newrow <- cbind(names(df[i]),newrow)
		tb <- rbind(tb, newrow)
	}
	return (tb)
}


testdata <- data %>% select(c(age, educ, income, Hj_conx_teachers, Hk_conx_avail, Hi_conx_ownlearn, migrate, perc_liv_yrs, Bg_liv_yrs, fishincome, yrfish, yrorg,Ce_fish_days, Hc_locus_Rcoded, Hf_locus_Rcoded, Ga_QoL, Ge_resil_catas, Gg_resil_abuse, Hc_bel_accept, socap_num, Hg_trust_membs, Hd_socap_friend, Ib_monrisk, Kb_monitor, Ke_clearact, Kf_poach, Ki_newidea, CCM_competence))


results_table <- multi(data$Game_buycount, testdata, 'pearson', 'greater') ; results_table

write.csv(results_table, file = "pearson_greater.csv")




